package com.jiulong.springboot_validator.controller;

import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.jiulong.springboot_validator.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class FileController {
    @Autowired
    private UploadService uploadService;
    @Autowired
    private FastFileStorageClient fastFileStorageClient;

    @RequestMapping("/upload")
    @ResponseBody
    public Map<String, Object> upload(MultipartFile file) {
        Map<String, Object> map = new HashMap<>();
        String path = this.uploadService.uploadImage(file);
        map.put("path", path);
        return map;
    }

    @RequestMapping("/download")
    @ResponseBody
    public void download() {
        try {
            byte[] bytes = fastFileStorageClient.downloadFile("group1", "M00/00/00/wKgagGE41_OAB1h8AABcAPxUgUU240.xls", new DownloadByteArray());
            FileOutputStream stream = new FileOutputStream("服务器.xls");
            stream.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
